package com.rzico.sbl.ui.manager

import android.widget.TextView
import com.jeremyliao.liveeventbus.LiveEventBus
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCouponAddBinding
import com.rzico.sbl.model.BaseUrl
import com.rzico.sbl.viewmodel.CouponViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.other.DecimalNumberFilter
import com.xinnuo.common.helper.isBefore
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.utils.showListDialog
import com.xinnuo.common_ui.utils.showTimeDialog

class CouponAddActivity : BaseActivity(R.layout.activity_coupon_add) {

    private val mBinding by lazy { ActivityCouponAddBinding.bind(getRootView()) }
    private var mTypeId = ""
    private var mRangeId = ""
    private var mBeginDate = ""
    private var mEndDate = ""

    override fun getViewModel() = get<CouponViewModel>()

    override fun initData() {
        initTitle(title = "新增优惠券")

        mBinding
            .apply {
                couponCount.filters = arrayOf(DecimalNumberFilter())
                couponTiao.filters = arrayOf(DecimalNumberFilter())
            }
    }

    override fun initListener() {
        mBinding.apply {
            couponTypeLl.oneClick {
                showListDialog(
                    title = "请选择类型",
                    index = getViewModel().mTypeList.indexOfFirst { it.id == mTypeId },
                    items = getViewModel().mTypeList.map { it.name },
                    listener = { index, hint ->
                        mTypeId = getViewModel().mTypeList[index].id
                        couponType.text = hint

                        when (index) {
                            0 -> {
                                couponCountHint.text = "优惠面额"
                                couponCount.hint = "请输入优惠面额"
                            }
                            1 -> {
                                couponCountHint.text = "优惠折扣"
                                couponCount.hint = "请输入优惠折扣"
                            }
                        }
                    }
                )
            }

            couponRangeLl.oneClick {
                showListDialog(
                    title = "请选择范围",
                    index = getViewModel().mRangeList.indexOfFirst { it.id == mRangeId },
                    items = getViewModel().mRangeList.map { it.name },
                    listener = { index, hint ->
                        mRangeId = getViewModel().mRangeList[index].id
                        couponRange.text = hint
                    }
                )
            }

            couponStart.oneClick {
                showTimeDialog(
                    title = "开始时间",
                    count = 3,
                    isCurrentDate = true,
                    isBeforeLimited = true
                ) { _, _, _, _, _, hint ->
                    it as TextView
                    mBeginDate = hint
                    it.text = hint
                }
            }

            couponEnd.oneClick {
                showTimeDialog(
                    title = "结束时间",
                    count = 3,
                    isCurrentDate = true,
                    isBeforeLimited = true
                ) { _, _, _, _, _, hint ->
                    it as TextView
                    mEndDate = hint
                    it.text = hint
                }
            }

            btSave.oneClick {
                when {
                    mTypeId.isEmpty() -> {
                        showToast("请选择优惠类型")
                        return@oneClick
                    }
                    mRangeId.isEmpty() -> {
                        showToast("请选择使用范围")
                        return@oneClick
                    }
                    couponTotal.text.isEmpty() -> {
                        showToast("请输入发行张数")
                        couponTotal.requestFocus()
                        return@oneClick
                    }
                    couponCount.text.isEmpty() -> {
                        showToast("请输入优惠面额")
                        couponCount.requestFocus()
                        return@oneClick
                    }
                    couponTiao.text.isEmpty() -> {
                        showToast("请输入使用条件")
                        couponTiao.requestFocus()
                        return@oneClick
                    }
                    mBeginDate.isEmpty() -> {
                        showToast("请选择开始时间")
                        return@oneClick
                    }
                    mEndDate.isEmpty() -> {
                        showToast("请选择结束时间")
                        return@oneClick
                    }
                    couponRule.textTrim.isEmpty() -> {
                        showToast("请输入规则介绍")
                        couponRule.requestFocus()
                        return@oneClick
                    }
                    mEndDate.isBefore(mBeginDate) -> {
                        showToast("开始日期不能大于结束日期")
                        return@oneClick
                    }
                }

                getViewModel()
                    .request(
                        requestUrl = BaseUrl.mngCouponAdd,
                        isMultipart = true,
                        params = arrayOf(
                            "type" to mTypeId,
                            "scope" to mRangeId,
                            "stock" to couponTotal.content,
                            "amount" to couponCount.content,
                            "minimumPrice" to couponTiao.content,
                            "beginDate" to mBeginDate,
                            "endDate" to mEndDate,
                            "introduction" to couponRule.textTrim
                        ),
                        onSuccess = {
                            showToast("优惠券添加成功！")

                            if (intent.getExtra("status") == "list") {
                                // 更新优惠券列表
                                LiveEventBus
                                    .get("couponList")
                                    .post("refresh")
                            }

                            finishView()
                        }
                    )
            }
        }
    }

}